package com.crrcdt.vue_project.util;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.crrcdt.vue_project.entity.Category;
import com.crrcdt.vue_project.entity.Permission;
import com.crrcdt.vue_project.entity.vo.CateVo;
import com.crrcdt.vue_project.service.CategoryService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class TreeUtils {
    //Permission权限树形列表
    public static List<Tree<String>> getTrees(List<Permission> permissions) {
        final Map<Integer, String> map = permissions.stream().collect(Collectors.toMap(Permission::getPsId, Permission::getPath));
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        permissions.stream().forEach(entity -> {
            nodeList.add(new TreeNode<>(entity.getPsId().toString(), entity.getParentId().toString(), entity.getPsName(), null));
        });
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
// 自定义属性名 都要默认值的
        treeNodeConfig.setWeightKey("order");
        treeNodeConfig.setIdKey("id");
// 最大递归深度
        treeNodeConfig.setDeep(3);
        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
                    tree.setWeight(treeNode.getWeight());
                    tree.setName(treeNode.getName());
                    final String id = treeNode.getId();
                    // 扩展属性 ...
                    tree.putExtra("path", map.get(Integer.parseInt(treeNode.getId())));
                });
        // 0表示最顶层的id是0
        return treeNodes;
    }
    //Cate分类树形列表
    public static List<Tree<String>> getCateTrees(List<Category> categories, Map<Integer, CateVo> idAndStrMap) {
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        categories.stream().forEach(entity -> {
            nodeList.add(new TreeNode<>(entity.getCatId().toString(), entity.getCatPid().toString(), entity.getCatName(), null));
        });
//        final List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");
//        System.out.println(treeList);
//        return treeList;
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
//        // 自定义属性名 都要默认值的
//        treeNodeConfig.setWeightKey("order");
//        treeNodeConfig.setIdKey("rid");
      // 最大递归深度
        treeNodeConfig.setDeep(3);
        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
                    tree.setWeight(treeNode.getWeight());
                    tree.setName(treeNode.getName());
                    final CateVo cateVo = idAndStrMap.get(Integer.parseInt(tree.getId()));
                    // 扩展属性 ...
                    tree.putExtra("level", cateVo.getCatLevel());
                    tree.putExtra("deleted", cateVo.getCatDeleted());
                    tree.putExtra("catId", tree.getId());

                });
        return treeNodes;
    }

}